<html>
	<head></head>
	<body>	
		<?php 
			$db = mysql_connect('oniddb.cws.oregonstate.edu','rundej-db','Hp2tYI0CxEY4U9J9');
			if(mysql_errno($db)){
				echo mysql_errno($db);
			}
			mysql_select_db('rundej-db',$db);
			
			if($_POST["playerID"])
			{
				if(!$_POST["deltaRuns"])
					$_POST["deltaRuns"] = 0;
				if(!$_POST["deltaWickets"])
					$_POST["deltaWickets"] = 0;
				if(!$_POST["deltaOvers"])
					$_POST["deltaOvers"] = 0;
				if(!$_POST["deltaInnings"])
					$_POST["deltaInnings"] = 0;
				$query = "Select * from Players where Number = ".$_POST["playerID"];
				$result = mysql_query($query,$db);
				if(mysql_num_rows($result) == 1)
				{
					$player = mysql_fetch_array($result);
					if(!$_POST["position"])
						$_POST["position"] = $player["Primary_Position"];
						
					if($_POST["region"])
					{
						$query = "Select * from Region where Region_No = ".$_POST["region"];
						$result = mysql_query($query,$db);
						if(mysql_num_rows($result) == 0)
						{
							echo "Invalid Region Number";
							$_POST["region"] = $player["Region_No"];
						}
					}
					if(!$_POST["region"])
						$_POST["region"] = $player["Region_No"];
					
					$query = "Update Players Set Innings_Batted = Innings_Batted + ".$_POST["deltaInnings"].", Wickets_Taken = Wickets_Taken + ".$_POST["deltaWickets"].", Overs_Bowled = Overs_Bowled + ".$_POST["deltaOvers"].", Runs_Scored = Runs_Scored + ".$_POST["deltaRuns"].", Primary_Position = '".$_POST["position"]."', Region_No = ".$_POST["region"]." where Number = ".$_POST["playerID"];
					mysql_query($query,$db);
					echo "Update Successful";
					
					?>
					<table border = "5">
						<tr>
							<th>Number</th>
							<th>Fname</th>
							<th>Lname</th>
							<th>Region Name</th>
							<th>Position</th>
							<th>Innings Batted</th>
							<th>Overs Bowled</th>
							<th>Wickets Taken</th>
							<th>Runs Scored</th>
						</tr>
					<?php
					$query = "Select * from Players natural join Region where Number = ".$_POST["playerID"];
					$result = mysql_query($query,$db);
					?><tr><?php
					$row = mysql_fetch_array($result);
					?><td><?php
					echo $row["Number"];
					?></td><td><?php
					echo $row["Fname"];
					?></td><td><?php
					echo $row["Lname"];
					?></td><td><?php
					echo $row["Region_Name"];
					?></td><td><?php
					echo $row["Primary_Position"];
					?></td><td><?php
					echo $row["Innings_Batted"];
					?></td><td><?php
					echo $row["Overs_Bowled"];
					?></td><td><?php
					echo $row["Wickets_Taken"];
					?></td><td><?php
					echo $row["Runs_Scored"];			
					?></td></tr></table><?php						
				}
				else
					echo "Error, Player not found.";
			}
			else
				echo "Error, no player ID given </br>";
		?>
		
		<a href="players.php">Return to Players page</a>
	</body>
</html>